<html>
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"/>
  <title>
   Python 函数手册大全！  | 数螺 | NAUT IDEA
  </title>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet"/>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
  <style type="text/css">
   #xmain img {
                  max-width: 100%;
                  display: block;
                  margin-top: 10px;
                  margin-bottom: 10px;
                }

                #xmain p {
                    line-height:150%;
                    font-size: 16px;
                    margin-top: 20px;
                }

                #xmain h2 {
                    font-size: 24px;
                }

                #xmain h3 {
                    font-size: 20px;
                }

                #xmain h4 {
                    font-size: 18px;
                }


                .header {
	           background-color: #0099ff;
	           color: #ffffff;
	           margin-bottom: 20px;
	        }

	        .header p {
                  margin: 0px;
                  padding: 10px 0;
                  display: inline-block;  
                  vertical-align: middle;
                  font-size: 16px;
               }

               .header a {
                 color: white;
               }

              .header img {
                 height: 25px;
              }
  </style>
  <script src="http://cdn.bootcss.com/jquery/3.0.0/jquery.min.js">
  </script>
  <script src="http://nautstatic-10007657.file.myqcloud.com/static/css/readability.min.js" type="text/javascript">
  </script>
  <script type="text/javascript">
   $(document).ready(function() {
                 var loc = document.location;
                 var uri = {
                  spec: "http://dataunion.org/19838.html",
                  host: "http://dataunion.org",
                  prePath: "http://dataunion.org",
                  scheme: "http",
                  pathBase: "http://dataunion.org/"
                 };
    
                 var documentClone = document.cloneNode(true);
                 var article = new Readability(uri, documentClone).parse();
     
                 document.getElementById("xmain").innerHTML = article.content;
                });
  </script>
  <!-- 1466461329: Accept with keywords: (title(0.4):Python,社区,数盟,函数,手册, topn(0.366666666667):定义,数盟,python,数据挖掘,空间,参数值,表达式,变量,关键字,Python,缺省,数量,嵌套,例子,参数,文章,命名,日志,默认值,属性,教程,函数,对象,元组,字符串,功能,字典,函数调用,列表,数据结构).-->
 </head>
 <body onload="">
  <div class="header">
   <div class="container">
    <div class="row">
     <div class="col-xs-6 col-sm-6 text-left">
      <a href="/databee">
       <img src="http://nautidea-10007657.cos.myqcloud.com/logo_white.png"/>
      </a>
      <a href="/databee">
       <p>
        数螺
       </p>
      </a>
     </div>
     <div class="hidden-xs col-sm-6 text-right">
      <p>
       致力于数据科学的推广和知识传播
      </p>
     </div>
    </div>
   </div>
  </div>
  <div class="container text-center">
   <h1>
    Python 函数手册大全！
   </h1>
  </div>
  <div class="container" id="xmain">
   ﻿﻿
   <title>
    Python 函数手册大全！ | 数盟社区
   </title>
   <!-- All in One SEO Pack 2.2.7.6.2 by Michael Torbert of Semper Fi Web Design[32,61] -->
   <!-- /all in one seo pack -->
   <!--
<div align="center">
<a href="http://strata.oreilly.com.cn/hadoop-big-data-cn?cmp=mp-data-confreg-home-stcn16_dataunion_pc" target="_blank"><img src="http://dataunion.org/wp-content/uploads/2016/05/stratabj.jpg"/ ></a>
</div>
-->
   <header id="header-web">
    <div class="header-main">
     <hgroup class="logo">
      <h1>
       <a href="http://dataunion.org/" rel="home" title="数盟社区">
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/logo.png"/>
       </a>
      </h1>
     </hgroup>
     <!--logo-->
     <nav class="header-nav">
      <ul class="menu" id="menu-%e4%b8%bb%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-71" id="menu-item-71">
        <a href="http://dataunion.org/category/events" title="events">
         活动
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22457" id="menu-item-22457">
          <a href="http://dataunion.org/2016timeline">
           2016档期
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22459" id="menu-item-22459">
          <a href="http://dataunion.org/category/parterc">
           合作会议
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-has-children menu-item-20869" id="menu-item-20869">
        <a href="http://dataunion.org/category/tech" title="articles">
         文章
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20867" id="menu-item-20867">
          <a href="http://dataunion.org/category/tech/base" title="base">
           基础架构
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3302" id="menu-item-3302">
          <a href="http://dataunion.org/category/tech/ai" title="ai">
           人工智能
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3303" id="menu-item-3303">
          <a href="http://dataunion.org/category/tech/analysis" title="analysis">
           数据分析
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21920" id="menu-item-21920">
          <a href="http://dataunion.org/category/tech/dm">
           数据挖掘
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3314" id="menu-item-3314">
          <a href="http://dataunion.org/category/tech/viz" title="viz">
           可视化
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3305" id="menu-item-3305">
          <a href="http://dataunion.org/category/tech/devl" title="devl">
           编程语言
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-20876" id="menu-item-20876">
        <a href="http://dataunion.org/category/industry">
         行业
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-16328" id="menu-item-16328">
          <a href="http://dataunion.org/category/industry/case" title="case">
           行业应用
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2112" id="menu-item-2112">
          <a href="http://dataunion.org/category/industry/demo" title="demo">
           Demo展示
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21562" id="menu-item-21562">
          <a href="http://dataunion.org/category/industry/news">
           行业资讯
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-311" id="menu-item-311">
        <a href="http://dataunion.org/category/sources" title="sources">
         资源
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20870" id="menu-item-20870">
        <a href="http://dataunion.org/category/books" title="book">
         图书
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21363" id="menu-item-21363">
        <a href="http://dataunion.org/category/training">
         课程
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-21853" id="menu-item-21853">
        <a href="http://dataunion.org/category/jobs">
         职位
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22050" id="menu-item-22050">
          <a href="http://dataunion.org/category/career">
           职业规划
          </a>
         </li>
        </ul>
       </li>
      </ul>
     </nav>
     <!--header-nav-->
    </div>
   </header>
   <!--header-web-->
   <div id="main">
    <div id="soutab">
     <form action="http://dataunion.org/" class="search" method="get">
     </form>
    </div>
    <div id="container">
     <nav id="mbx">
      当前位置：
      <a href="http://dataunion.org">
       首页
      </a>
      &gt;
      <a href="http://dataunion.org/category/tech">
       文章
      </a>
      &gt;  正文
     </nav>
     <!--mbx-->
     <article class="content">
      <header align="centre" class="contenttitle">
       <div class="mscc">
        <h1 class="mscctitle">
         <a href="http://dataunion.org/19838.html">
          Python 函数手册大全！
         </a>
        </h1>
        <address class="msccaddress ">
         <em>
          2,027 次阅读 -
         </em>
         <a href="http://dataunion.org/category/tech" rel="category tag">
          文章
         </a>
        </address>
       </div>
      </header>
      <div class="content-text">
       <blockquote>
        <p>
         出处：
         <a href="http://python.jobbole.com/81646/">
          伯乐在线
         </a>
        </p>
       </blockquote>
       <p>
        Python中的函数，无论是命名函数，还是匿名函数，都是语句和表达式的集合。在Python中，函数是第一个类对象，这意味着函数的用法并没有限制。Python函数的使用方式就像Python中其他值一样，例如字符串和数字等。Python函数拥有一些属性，通过使用Python内置函数dir就能查看这些属性，如下代码所示：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_435253">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                square(x):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                x
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python value">
                2
               </code>
              </div>
              <div class="line number3 index2 alt2">
              </div>
              <div class="line number4 index3 alt1">
               <code class="python plain">
                &gt;&gt;&gt; square
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python plain">
                &lt;function square at
               </code>
               <code class="python value">
                0x031AA230
               </code>
               <code class="python plain">
                &gt;
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python plain">
                &gt;&gt;&gt;
               </code>
               <code class="python functions">
                dir
               </code>
               <code class="python plain">
                (square)
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python plain">
                [
               </code>
               <code class="python string">
                '__call__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__class__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__closure__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__code__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__defaults__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__delattr__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__dict__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__doc__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__format__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__get__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__getattribute__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__globals__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__hash__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__init__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__module__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__name__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__new__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__reduce__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__reduce_ex__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__repr__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__setattr__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__sizeof__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__str__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__subclasshook__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                'func_closure'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                'func_code'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                'func_defaults'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                'func_dict'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                'func_doc'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                'func_globals'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                'func_name'
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python plain">
                &gt;&gt;&gt;
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        其中，一些重要的函数属性包括以下几个：
       </p>
       <p>
        1. __doc__返回指定函数的文档字符串。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_893508">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                square(x):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python comments">
                """return square of given number"""
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                x
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python value">
                2
               </code>
              </div>
              <div class="line number4 index3 alt1">
              </div>
              <div class="line number5 index4 alt2">
               <code class="python plain">
                &gt;&gt;&gt; square.__doc__
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python string">
                'return square of given number'
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        2. __name__返回函数名字。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_553528">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                square(x):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python comments">
                """return square of given number"""
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                x
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python value">
                2
               </code>
              </div>
              <div class="line number4 index3 alt1">
              </div>
              <div class="line number5 index4 alt2">
               <code class="python plain">
                &gt;&gt;&gt; square.func_name
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python string">
                'square'
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        3. __module__返回函数定义所在模块的名字。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_805581">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                square(x):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python comments">
                """return square of given number"""
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                x
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python value">
                2
               </code>
              </div>
              <div class="line number4 index3 alt1">
              </div>
              <div class="line number5 index4 alt2">
               <code class="python plain">
                &gt;&gt;&gt; square.__module__
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python string">
                '__main__'
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        4. func_defaults返回一个包含默认参数值的元组，默认参数将在后文进行讨论。
       </p>
       <p>
        5. func_globals返回一个包含函数全局变量的字典引用。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_3491">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                square(x):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python comments">
                """return square of given number"""
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                x
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python value">
                2
               </code>
              </div>
              <div class="line number4 index3 alt1">
              </div>
              <div class="line number5 index4 alt2">
               <code class="python plain">
                &gt;&gt;&gt; square.func_globals
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python plain">
                {
               </code>
               <code class="python string">
                '__builtins__'
               </code>
               <code class="python plain">
                : &lt;module
               </code>
               <code class="python string">
                '__builtin__'
               </code>
               <code class="python plain">
                (built
               </code>
               <code class="python keyword">
                -
               </code>
               <code class="python keyword">
                in
               </code>
               <code class="python plain">
                )&gt;,
               </code>
               <code class="python string">
                '__name__'
               </code>
               <code class="python plain">
                :
               </code>
               <code class="python string">
                '__main__'
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                'square'
               </code>
               <code class="python plain">
                : &lt;function square at
               </code>
               <code class="python value">
                0x10f099c08
               </code>
               <code class="python plain">
                &gt;,
               </code>
               <code class="python string">
                '__doc__'
               </code>
               <code class="python plain">
                :
               </code>
               <code class="python color1">
                None
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                '__package__'
               </code>
               <code class="python plain">
                :
               </code>
               <code class="python color1">
                None
               </code>
               <code class="python plain">
                }
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        6. func_dict返回支持任意函数属性的命名空间。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_29706">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                square(x):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python comments">
                """return square of given number"""
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                x
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python value">
                2
               </code>
              </div>
              <div class="line number4 index3 alt1">
              </div>
              <div class="line number5 index4 alt2">
               <code class="python plain">
                &gt;&gt;&gt; square.func_dict
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python plain">
                {}
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        7. func_closure返回一个胞体元组，其中胞体包含了函数自由变量的绑定，闭包将在后文讨论。
       </p>
       <p>
        函数可以作为参数传递给其他函数。这些以其他函数作为参数的函数通常称为更高阶函数，这就构成了函数式编程中一个非常重要的部分。高阶函数一个很好的例子就是map函数，该函数接受一个函数和一个迭代器作为参数，并将函数应用于迭代器中的每一项，最后返回一个新的列表。我们将在下面的例子中演示这一点，例子中将前面定义的square函数和一个数字迭代器传递给map函数。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_313358">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                &gt;&gt;&gt;
               </code>
               <code class="python functions">
                map
               </code>
               <code class="python plain">
                (square,
               </code>
               <code class="python functions">
                range
               </code>
               <code class="python plain">
                (
               </code>
               <code class="python value">
                10
               </code>
               <code class="python plain">
                ))
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python plain">
                [
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python value">
                4
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python value">
                9
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python value">
                16
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python value">
                25
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python value">
                36
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python value">
                49
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python value">
                64
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python value">
                81
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        此外，函数也可以在其他函数代码块内部定义，同时也能从其他函数调用中返回。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_684916">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                outer():
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                outer_var
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                "outer variable"
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                inner():
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                outer_var
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                inner
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        在上面的例子中，我们在函数outer中定义了另一个函数inner，并且当函数outer执行时将返回inner函数。此外，像任何其他Python对象一样，函数也可以赋值给变量，如下所示：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_874842">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                outer():
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                outer_var
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                "outer variable"
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                inner():
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                outer_var
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                inner
               </code>
              </div>
              <div class="line number6 index5 alt1">
              </div>
              <div class="line number7 index6 alt2">
               <code class="python plain">
                &gt;&gt;&gt; func
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                outer()
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python plain">
                &gt;&gt;&gt; func
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python plain">
                &lt;function inner at
               </code>
               <code class="python value">
                0x031AA270
               </code>
               <code class="python plain">
                &gt;
               </code>
              </div>
              <div class="line number10 index9 alt1">
               <code class="python plain">
                &gt;&gt;&gt;
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        在上面的例子中，outer函数被调用时将会返回一个函数，并将返回的函数赋值给变量func。最后，该变量就可以像被返回的函数一样被调用：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_760972">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                &gt;&gt;&gt; func()
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python string">
                'outer variable'
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <h2>
        函数定义
       </h2>
       <p>
        关键字def用于创建用户自定义函数，函数定义就是一些可执行的语句。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_369766">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                square(x):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                x
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python value">
                2
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        在上面的square函数中，当包含该函数的模块加载到Python解释器中时，或者如果该函数在Python REPL中定义，那么将会执行函数定义语句def square(x)。然而，这对以可变数据结构作为值的默认参数有一些影响，这一点我们将会在后文讲述。函数定义的执行会绑定当前本地命名空间中的函数名（可以将命名空间当作名字到值的一种映射，并且这种映射还可以嵌套，命名空间和范围会在另一个教程中详细介绍）到一个函数对象，该对象是一个对函数中可执行代码的包装器。这个函数对象包含了一个对当前全局命名空间的引用，而当前命名空间指该函数调用时所使用的全局命名空间。此外，函数定义不会执行函数体，只有在函数被调用时才会执行函数体。
       </p>
       <h2>
        函数调用参数
       </h2>
       <p>
        除了正常的参数之外，Python函数还支持数量可变的参数。这些参数有主要有下面描述的三种类别：
       </p>
       <p>
        <strong>
         1. 默认参数值：
        </strong>
        这允许用户为函数的参数定义一些默认值。这种情况下，可以以更少的参数来调用该函数，而函数调用时未提供的参数，Python会使用默认提供的值作为这些参数值。下面的例子展示了这种用法：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_3534">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                show_args(arg, def_arg
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                , def_arg2
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                2
               </code>
               <code class="python plain">
                ):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python string">
                "arg={}, def_arg={}, def_arg2={}"
               </code>
               <code class="python plain">
                .
               </code>
               <code class="python functions">
                format
               </code>
               <code class="python plain">
                (arg, def_arg, def_arg2)
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        上面例子函数的定义中，包含一个正常位置的参数arg和两个默认参数def_arg和def_arg2。该函数可以以下面中的任何一种方式进行调用：
       </p>
       <p>
        （1）只提供非缺省位置参数值。在本例中，缺省参数取默认值：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_581139">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                show_args(arg, def_arg
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                , def_arg2
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                2
               </code>
               <code class="python plain">
                ):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python string">
                "arg={}, def_arg={}, def_arg2={}"
               </code>
               <code class="python plain">
                .
               </code>
               <code class="python functions">
                format
               </code>
               <code class="python plain">
                (arg, def_arg, def_arg2)
               </code>
              </div>
              <div class="line number3 index2 alt2">
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                &gt;&gt;&gt; show_args(
               </code>
               <code class="python string">
                "tranquility"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python string">
                'arg=tranquility, def_arg=1, def_arg2=2'
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        （2）用提供的值覆盖一些默认的参数值，包括非缺省位置参数：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_89204">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                show_args(arg, def_arg
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                , def_arg2
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                2
               </code>
               <code class="python plain">
                ):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python string">
                "arg={}, def_arg={}, def_arg2={}"
               </code>
               <code class="python plain">
                .
               </code>
               <code class="python functions">
                format
               </code>
               <code class="python plain">
                (arg, def_arg, def_arg2)
               </code>
              </div>
              <div class="line number3 index2 alt2">
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                &gt;&gt;&gt; show_args(
               </code>
               <code class="python string">
                "tranquility"
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                "to Houston"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python string">
                'arg=tranquility, def_arg=to Houston, def_arg2=2'
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        （3）为所有参数提供值，可以用这些值覆盖默认参数值：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_469248">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                show_args(arg, def_arg
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                , def_arg2
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                2
               </code>
               <code class="python plain">
                ):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python string">
                "arg={}, def_arg={}, def_arg2={}"
               </code>
               <code class="python plain">
                .
               </code>
               <code class="python functions">
                format
               </code>
               <code class="python plain">
                (arg, def_arg, def_arg2)
               </code>
              </div>
              <div class="line number3 index2 alt2">
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                &gt;&gt;&gt; show_args(
               </code>
               <code class="python string">
                "tranquility"
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                "to Houston"
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                "the eagle has landed"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python string">
                'arg=tranquility, def_arg=to Houston, def_arg2=the eagle has landed'
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        当使用可变的默认数据结构作为默认参数时，需要特别小心。因为函数定义只执行一次，所以这些可变的数据结构（引用值）只在函数定义时创建一次。这就意味着，相同的可变数据结构将用于所有函数调用，如下面例子所示：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_608368">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                show_args_using_mutable_defaults(arg, def_arg
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                []):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                def_arg.append(
               </code>
               <code class="python string">
                "Hello World"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python string">
                "arg={}, def_arg={}"
               </code>
               <code class="python plain">
                .
               </code>
               <code class="python functions">
                format
               </code>
               <code class="python plain">
                (arg, def_arg)
               </code>
              </div>
              <div class="line number4 index3 alt1">
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                &gt;&gt;&gt; show_args_using_mutable_defaults(
               </code>
               <code class="python string">
                "test"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python spaces">
               </code>
               <code class="python string">
                "arg=test, def_arg=['Hello World']"
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                &gt;&gt;&gt; show_args_using_mutable_defaults(
               </code>
               <code class="python string">
                "test 2"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python spaces">
               </code>
               <code class="python string">
                "arg=test 2, def_arg=['Hello World', 'Hello World']"
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        在每个函数调用中，“Hello World”都被添加到了def_arg列表中，在调用两次函数之后，默认参数中将有两个“Hello World”字符串。当使用可变默认参数作为默认值时，注意到这一点非常重要。当我们讨论Python数据模型时，将会清楚理解其原因。
       </p>
       <p>
        <strong>
         2. 关键字参数：
        </strong>
        以“kwarg=value”的形式使用关键字参数也可以调用函数。其中，kwarg指函数定义中使用的参数名称。以下面定义的含有默认和非默认参数的函数为例：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_501360">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                show_args(arg, def_arg
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python string">
                "arg={}, def_arg={}"
               </code>
               <code class="python plain">
                .
               </code>
               <code class="python functions">
                format
               </code>
               <code class="python plain">
                (arg, def_arg)
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        为了演示使用关键字参数调用函数，下面的函数可以以后面的任何一种方式调用：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_775832">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                show_args(arg
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                "test"
               </code>
               <code class="python plain">
                , def_arg
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                3
               </code>
               <code class="python plain">
                )
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_840066">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                show_args(test)
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_346769">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                show_args(arg
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                "test"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_977471">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                show_args(
               </code>
               <code class="python string">
                "test"
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python value">
                3
               </code>
               <code class="python plain">
                )
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        在函数调用中，关键字参数不得早于非关键字参数，所以以下调用会失败：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_175331">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                show_args(def_arg
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                4
               </code>
               <code class="python plain">
                )
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        函数不能为一个参数提供重复值，所以下面的调用方法是非法的：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_679816">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                show_args(
               </code>
               <code class="python string">
                "test"
               </code>
               <code class="python plain">
                , arg
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                "testing"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        在上面的例子中，参数arg是位置参数，所以值“test”会分配给它。而试图将其再次分配给关键字arg，意味着在尝试多重赋值，而这是非法的。
       </p>
       <p>
        传递的所有关键字参数必须匹配一个函数接受的参数，而包含非可选参数的关键字顺序并不重要，所以下面调换了参数顺序的写法是合法的：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_81809">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                show_args(def_arg
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                "testing"
               </code>
               <code class="python plain">
                , arg
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                "test"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        <strong>
         3. 任意的参数列表：
        </strong>
        Python还支持定义这样的函数，该函数可以接受以元组形式传递的任意数量的参数，Python教程中的一个例子如下所示：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_894515">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                write_multiple_items(
               </code>
               <code class="python functions">
                file
               </code>
               <code class="python plain">
                , separator,
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python plain">
                args):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python functions">
                file
               </code>
               <code class="python plain">
                .write(separator.join(args))
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        任意数量的参数必须在正常参数之后。在本例中，任意数量参数存在于参数file和separator之后。下面是一个调用上述定义函数的示例：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_554430">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                f
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python functions">
                open
               </code>
               <code class="python plain">
                (
               </code>
               <code class="python string">
                "test.txt"
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                "wb"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python plain">
                write_multiple_items(f,
               </code>
               <code class="python string">
                " "
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                "one"
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                "two"
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                "three"
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                "four"
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                "five"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        上面的参数one、two、three、four、five捆绑在一起共同组成了一个元组，通过参数args就能访问该元组。
       </p>
       <h2>
        解包函数参数
       </h2>
       <p>
        有时候，函数调用的参数可能是以元组、列表或字典的形式存在。可以通过使用“*”或“**”操作符将这些参数解包到函数内部以供调用。以下面的函数为例，该函数接受两个位置参数，并打印出两个参数的值。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_47367">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                print_args(a, b):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python plain">
                a
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python plain">
                b
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        如果提供给函数的参数值是以列表形式存在，那么我们可以直接将这些值解包到函数中，如下所示：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_787347">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                &gt;&gt;&gt; args
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                [
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python value">
                2
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python plain">
                &gt;&gt;&gt; print_args(
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python plain">
                args)
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python value">
                1
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python value">
                2
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        类似的，当我们有关键词时，可以使用字典来存储kwarg到值的映射关系，并利用“**”操作符将关键字参数解包到函数，如下所示：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_434588">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                &gt;&gt;&gt;
               </code>
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                parrot(voltage, state
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                ’a stiff’, action
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                ’voom’):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                print
               </code>
               <code class="python string">
                "-- This parrot wouldn’t"
               </code>
               <code class="python plain">
                , action,
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                "if you put"
               </code>
               <code class="python plain">
                , voltage,
               </code>
               <code class="python string">
                "volts through it."
               </code>
               <code class="python plain">
                ,
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                print
               </code>
               <code class="python string">
                "E’s"
               </code>
               <code class="python plain">
                , state,
               </code>
               <code class="python string">
                "!"
               </code>
              </div>
              <div class="line number5 index4 alt2">
              </div>
              <div class="line number6 index5 alt1">
               <code class="python plain">
                &gt;&gt;&gt; d
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                {
               </code>
               <code class="python string">
                "voltage"
               </code>
               <code class="python plain">
                :
               </code>
               <code class="python string">
                "four million"
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                "state"
               </code>
               <code class="python plain">
                :
               </code>
               <code class="python string">
                "bleedin’ demised"
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python string">
                "action"
               </code>
               <code class="python plain">
                :
               </code>
               <code class="python string">
                "VOOM"
               </code>
               <code class="python plain">
                }
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python plain">
                &gt;&gt;&gt; parrot(
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python plain">
                d)
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python plain">
                &gt;&gt;&gt; This parrot wouldn’t VOOM
               </code>
               <code class="python keyword">
                if
               </code>
               <code class="python plain">
                you put four million volts through it. E’s bleedin’ demised
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <h2>
        利用“*”和“**”定义函数
       </h2>
       <p>
        有时候，当定义一个函数时，我们之前可能不知道参数的数量。这就导致了下面签名的函数定义：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_733440">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                show_args(arg,
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python plain">
                args,
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python plain">
                kwargs)
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        “*args”参数表示未知的位置参数序列长度，而“**kwargs”代表包含关键字和值映射关系的字典，它可以包含任意数量的关键字和值映射，并且在函数定义中“*args”必须位于“**kwargs”前面。下面的代码演示了这种情况：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_638197">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
             <div class="line number14 index13 alt1">
              14
             </div>
             <div class="line number15 index14 alt2">
              15
             </div>
             <div class="line number16 index15 alt1">
              16
             </div>
             <div class="line number17 index16 alt2">
              17
             </div>
             <div class="line number18 index17 alt1">
              18
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                show_args(arg,
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python plain">
                args,
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python plain">
                kwargs):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python plain">
                arg
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                for
               </code>
               <code class="python plain">
                item
               </code>
               <code class="python keyword">
                in
               </code>
               <code class="python plain">
                args:
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                print
               </code>
               <code class="python plain">
                args
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                for
               </code>
               <code class="python plain">
                key, value
               </code>
               <code class="python keyword">
                in
               </code>
               <code class="python plain">
                kwargs:
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                print
               </code>
               <code class="python plain">
                key, value
               </code>
              </div>
              <div class="line number7 index6 alt2">
              </div>
              <div class="line number8 index7 alt1">
               <code class="python plain">
                &gt;&gt;&gt; args
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                [
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python value">
                2
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python value">
                3
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python value">
                4
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python plain">
                &gt;&gt;&gt; kwargs
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python functions">
                dict
               </code>
               <code class="python plain">
                (name
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                'testing'
               </code>
               <code class="python plain">
                , age
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                24
               </code>
               <code class="python plain">
                , year
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                2014
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number10 index9 alt1">
               <code class="python plain">
                &gt;&gt;&gt; show_args(
               </code>
               <code class="python string">
                "hey"
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python plain">
                args,
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python plain">
                kwargs)
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="python plain">
                hey
               </code>
              </div>
              <div class="line number12 index11 alt1">
               <code class="python value">
                1
               </code>
              </div>
              <div class="line number13 index12 alt2">
               <code class="python value">
                2
               </code>
              </div>
              <div class="line number14 index13 alt1">
               <code class="python value">
                3
               </code>
              </div>
              <div class="line number15 index14 alt2">
               <code class="python value">
                4
               </code>
              </div>
              <div class="line number16 index15 alt1">
               <code class="python plain">
                age
               </code>
               <code class="python value">
                24
               </code>
              </div>
              <div class="line number17 index16 alt2">
               <code class="python plain">
                name testing
               </code>
              </div>
              <div class="line number18 index17 alt1">
               <code class="python plain">
                year
               </code>
               <code class="python value">
                2014
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        必须向函数提供正常的参数，但“*args”和“**kwargs”却是可选的，如下所示：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_815080">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                &gt;&gt;&gt; show_args(
               </code>
               <code class="python string">
                "hey"
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python plain">
                args,
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python plain">
                kwargs)
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python plain">
                hey
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        在函数调用中，普通参数以正常方式提供，而可选参数则可以通过解包的形式到达函数调用中。
       </p>
       <h2>
        匿名函数
       </h2>
       <p>
        Python也支持匿名函数，这些函数使用lambda关键字创建。Python中Lambda表达式的形式如下所示：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_82864">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                lambda_expr ::
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                "lambda"
               </code>
               <code class="python plain">
                [parameter_list]: expression
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        Lambda表达式返回评估后的函数对象，并且具有与命名函数相同的属性。在Python中，Lambda表达式通常只用于非常简单的函数，如下所示：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_822550">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
             <div class="line number14 index13 alt1">
              14
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                &gt;&gt;&gt; square
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python keyword">
                lambda
               </code>
               <code class="python plain">
                x: x
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python value">
                2
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python plain">
                &gt;&gt;&gt;
               </code>
               <code class="python keyword">
                for
               </code>
               <code class="python plain">
                i
               </code>
               <code class="python keyword">
                in
               </code>
               <code class="python functions">
                range
               </code>
               <code class="python plain">
                (
               </code>
               <code class="python value">
                10
               </code>
               <code class="python plain">
                ):
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                square(i)
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python value">
                0
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python value">
                1
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python value">
                4
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python value">
                9
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python value">
                16
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python value">
                25
               </code>
              </div>
              <div class="line number10 index9 alt1">
               <code class="python value">
                36
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="python value">
                49
               </code>
              </div>
              <div class="line number12 index11 alt1">
               <code class="python value">
                64
               </code>
              </div>
              <div class="line number13 index12 alt2">
               <code class="python value">
                81
               </code>
              </div>
              <div class="line number14 index13 alt1">
               <code class="python plain">
                &gt;&gt;&gt;
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        上面的lambda表达式的功能与下面命名函数的功能相同：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_621453">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                square(x):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                x
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python keyword">
                *
               </code>
               <code class="python value">
                2
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <h2>
        嵌套函数和闭包
       </h2>
       <p>
        在一个函数内部定义函数就创建了嵌套函数，如下所示：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_342801">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                <code>
                </code>
                <code>
                 python
                </code>
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python plain">
                <code class="python spaces">
                </code>
                <code class="python keyword">
                 def
                </code>
                <code class="python plain">
                 outer():
                </code>
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python plain">
                <code class="python spaces">
                </code>
                <code class="python plain">
                 outer_var
                </code>
                <code class="python keyword">
                 =
                </code>
                <code class="python string">
                 "outer variable"
                </code>
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python plain">
                <code class="python spaces">
                </code>
                <code class="python keyword">
                 def
                </code>
                <code class="python plain">
                 inner():
                </code>
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python plain">
                <code class="python spaces">
                </code>
                <code class="python keyword">
                 return
                </code>
                <code class="python plain">
                 outer_var
                </code>
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python plain">
                <code class="python spaces">
                </code>
                <code class="python keyword">
                 return
                </code>
                <code class="python plain">
                 inner
                </code>
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python plain">
                <code class="python plain">
                </code>
                <code>
                </code>
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        在这种类型的函数定义中，函数inner只在函数outer内部有效，所以当内部函数需要被返回（移动到外部作用范围）或被传递给另一个函数时，使用嵌套函数通常比较方便。在如在上面的嵌套函数中，每次调用外部函数时都会创建一个新的嵌套函数实例，这是因为，在每次执行外部函数时，都会执行一次内部函数定义，而其函数体则不会被执行。
       </p>
       <p>
        嵌套函数可以访问创建它的环境，这是python函数定义语义的直接结果。一个结果是，外部函数中定义的变量可以在内部函数中引用，即使外部函数已经执行结束。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_701147">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                outer():
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                outer_var
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                "outer variable"
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                inner():
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                outer_var
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                inner
               </code>
              </div>
              <div class="line number6 index5 alt1">
              </div>
              <div class="line number7 index6 alt2">
               <code class="python plain">
                &gt;&gt;&gt; x
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                outer()
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python plain">
                &gt;&gt;&gt; x
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python plain">
                &lt;function inner at
               </code>
               <code class="python value">
                0x0273BCF0
               </code>
               <code class="python plain">
                &gt;
               </code>
              </div>
              <div class="line number10 index9 alt1">
               <code class="python plain">
                &gt;&gt;&gt; x()
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="python string">
                'outer variable'
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        当内部嵌套的函数引用外部函数中的变量时，我们说嵌套函数相对于引用变量是封闭的。我们可以使用函数对象的一个特殊属性“__closure__”来访问这个封闭的变量，如下所示：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_854551">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                &gt;&gt;&gt; cl
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                x.__closure__
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python plain">
                &gt;&gt;&gt; cl
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python plain">
                (&lt;cell at
               </code>
               <code class="python value">
                0x029E4470
               </code>
               <code class="python plain">
                :
               </code>
               <code class="python functions">
                str
               </code>
               <code class="python functions">
                object
               </code>
               <code class="python plain">
                at
               </code>
               <code class="python value">
                0x02A0FD90
               </code>
               <code class="python plain">
                &gt;,)
               </code>
              </div>
              <div class="line number4 index3 alt1">
              </div>
              <div class="line number5 index4 alt2">
               <code class="python plain">
                &gt;&gt;&gt; cl[
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ].cell_contents
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python string">
                'outer variable'
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        Python中的闭包有一个古怪的行为。在Python 2.x及更低版本中，指向不可变类型（例如字符串和数字）的变量不能在闭包内反弹。下面的例子说明了这一点:
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_348049">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                counter():
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                count
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                0
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                c():
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                count
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                1
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                count
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                c
               </code>
              </div>
              <div class="line number7 index6 alt2">
              </div>
              <div class="line number8 index7 alt1">
               <code class="python plain">
                &gt;&gt;&gt; c
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                counter()
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python plain">
                &gt;&gt;&gt; c()
               </code>
              </div>
              <div class="line number10 index9 alt1">
               <code class="python plain">
                Traceback (most recent call last):
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                File
               </code>
               <code class="python string">
                "&lt;stdin&gt;"
               </code>
               <code class="python plain">
                , line
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python keyword">
                in
               </code>
               <code class="python plain">
                &lt;module&gt;
               </code>
              </div>
              <div class="line number12 index11 alt1">
               <code class="python spaces">
               </code>
               <code class="python functions">
                File
               </code>
               <code class="python string">
                "&lt;stdin&gt;"
               </code>
               <code class="python plain">
                , line
               </code>
               <code class="python value">
                4
               </code>
               <code class="python plain">
                ,
               </code>
               <code class="python keyword">
                in
               </code>
               <code class="python plain">
                c
               </code>
              </div>
              <div class="line number13 index12 alt2">
               <code class="python plain">
                UnboundLocalError: local variable
               </code>
               <code class="python string">
                'count'
               </code>
               <code class="python plain">
                referenced before assignment
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        一个相当不可靠的解决方案是，使用一个可变类型来捕获闭包，如下所示：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_635971">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
             <div class="line number14 index13 alt1">
              14
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                counter():
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                count
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                [
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                c():
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                count[
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ]
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                1
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                count[
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                c
               </code>
              </div>
              <div class="line number7 index6 alt2">
              </div>
              <div class="line number8 index7 alt1">
               <code class="python plain">
                &gt;&gt;&gt; c
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                counter()
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python plain">
                &gt;&gt;&gt; c()
               </code>
              </div>
              <div class="line number10 index9 alt1">
               <code class="python value">
                1
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="python plain">
                &gt;&gt;&gt; c()
               </code>
              </div>
              <div class="line number12 index11 alt1">
               <code class="python value">
                2
               </code>
              </div>
              <div class="line number13 index12 alt2">
               <code class="python plain">
                &gt;&gt;&gt; c()
               </code>
              </div>
              <div class="line number14 index13 alt1">
               <code class="python value">
                3
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        Python 3引入了“nonlocal”关键字用来解决下面所示的闭包范围问题。在本教程命名空间一节中，我们更加详细地描述了这些古怪用法。
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_859916">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                counter():
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                count
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                0
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                c():
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                nonlocal count
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                count
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                1
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                count
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                c
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        闭包可以用来维持状态（与类作用不同），在一些简单的情况下，还可以提供一种简洁性与可读性比类更强的解决方案，我们使用
        <a href="http://tech.pro/tutorial/1512/python-decorators" target="_blank">
         tech_pro
        </a>
        中的一个日志例子来说明这一点。假设一个非常简单的日志API，它使用基于类的面向对象思想，并可以在不同级别上打印日志：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_336445">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                class
               </code>
               <code class="python plain">
                Log:
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                __init__(
               </code>
               <code class="python color1">
                self
               </code>
               <code class="python plain">
                , level):
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python color1">
                self
               </code>
               <code class="python plain">
                ._level
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                level
               </code>
              </div>
              <div class="line number4 index3 alt1">
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                __call__(
               </code>
               <code class="python color1">
                self
               </code>
               <code class="python plain">
                , message):
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                print
               </code>
               <code class="python plain">
                (
               </code>
               <code class="python string">
                "{}: {}"
               </code>
               <code class="python plain">
                .
               </code>
               <code class="python functions">
                format
               </code>
               <code class="python plain">
                (
               </code>
               <code class="python color1">
                self
               </code>
               <code class="python plain">
                ._level, message))
               </code>
              </div>
              <div class="line number7 index6 alt2">
              </div>
              <div class="line number8 index7 alt1">
               <code class="python plain">
                log_info
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                Log(
               </code>
               <code class="python string">
                "info"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python plain">
                log_warning
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                Log(
               </code>
               <code class="python string">
                "warning"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number10 index9 alt1">
               <code class="python plain">
                log_error
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                Log(
               </code>
               <code class="python string">
                "error"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        相同的功能也可以使用闭包来实现，如下所示：
       </p>
       <div>
        <div class="syntaxhighlighter notranslate python" id="highlighter_870761">
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                make_log(level):
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                _(message):
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                print
               </code>
               <code class="python plain">
                (
               </code>
               <code class="python string">
                "{}: {}"
               </code>
               <code class="python plain">
                .
               </code>
               <code class="python functions">
                format
               </code>
               <code class="python plain">
                (level, message))
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                return
               </code>
               <code class="python plain">
                _
               </code>
              </div>
              <div class="line number5 index4 alt2">
              </div>
              <div class="line number6 index5 alt1">
               <code class="python plain">
                log_info
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                make_log(
               </code>
               <code class="python string">
                "info"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python plain">
                log_warning
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                make_log(
               </code>
               <code class="python string">
                "warning"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python plain">
                log_error
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                make_log(
               </code>
               <code class="python string">
                "error"
               </code>
               <code class="python plain">
                )
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        可以看出，即使两个版本都实现了相同的功能，但基于闭包的版本更简洁、可读性更好。闭包在一个主要的Python函数“函数修饰符”中也扮演着很重要的角色，这是使用非常广泛的功能，我们将在接下来的教程讲解。
       </p>
       <p>
        如果文中你发现了任何错误、问题或者你有更好的话题想让我写出来，可以在Twitter上联系我（@obi_inc）。
       </p>
       <p>
        <a href="http://www.jobbole.com/members/pyper">
         PyPer
        </a>
        翻译
        <br/>
        英文出处：
        <a href="http://intermediatepythonista.com/the-function" target="_blank">
         intermediatepythonista.com
        </a>
       </p>
      </div>
      <div>
       <strong>
        注：转载文章均来自于公开网络，仅供学习使用，不会用于任何商业用途，如果侵犯到原作者的权益，请您与我们联系删除或者授权事宜，联系邮箱：contact@dataunion.org。转载数盟网站文章请注明原文章作者，否则产生的任何版权纠纷与数盟无关。
       </strong>
      </div>
      <!--content_text-->
      <div class="fenxian">
       <!-- JiaThis Button BEGIN -->
       <div class="jiathis_style_32x32">
        <p class="jiathis_button_weixin">
        </p>
        <p class="jiathis_button_tsina">
        </p>
        <p class="jiathis_button_qzone">
        </p>
        <p class="jiathis_button_cqq">
        </p>
        <p class="jiathis_button_tumblr">
        </p>
        <a class="jiathis jiathis_txt jtico jtico_jiathis" href="http://www.jiathis.com/share" target="_blank">
        </a>
        <p class="jiathis_counter_style">
        </p>
       </div>
       <!-- JiaThis Button END -->
      </div>
     </article>
     <!--content-->
     <!--相关文章-->
     <div class="xianguan">
      <div class="xianguantitle">
       相关文章！
      </div>
      <ul class="pic">
       <li>
        <a href="http://dataunion.org/20824.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/09/t018630756a7e263b33-300x165.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20824.html" rel="bookmark" title="如何判断一笔交易是否属于欺诈？你只是需要一点数据挖掘">
         如何判断一笔交易是否属于欺诈？你只是需要一点数据挖掘
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/20820.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/09/1-300x200.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20820.html" rel="bookmark" title="人们对Python在企业级开发中的10大误解">
         人们对Python在企业级开发中的10大误解
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/20811.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/09/t0133fcacae8523307b_副本-300x200.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20811.html" rel="bookmark" title="大神亲传：26条深度学习的金科玉律！">
         大神亲传：26条深度学习的金科玉律！
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/20808.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/09/640.webp-11-300x137.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20808.html" rel="bookmark" title="我们是如何在一张地图上表现86万个数据的">
         我们是如何在一张地图上表现86万个数据的
        </a>
       </li>
      </ul>
     </div>
     <!--相关文章-->
     <div class="comment" id="comments">
      <!-- You can start editing here. -->
      <!-- If comments are open, but there are no comments. -->
      <div class="title">
       期待你一针见血的评论，Come on！
      </div>
      <div id="respond">
       <p>
        不用想啦，马上
        <a href="http://dataunion.org/wp-login.php?redirect_to=http%3A%2F%2Fdataunion.org%2F19838.html">
         "登录"
        </a>
        发表自已的想法.
       </p>
      </div>
     </div>
     <!-- .nav-single -->
    </div>
    <!--Container End-->
    <aside id="sitebar">
     <div class="sitebar_list2">
      <div class="wptag">
       <span class="tagtitle">
        热门标签+
       </span>
       <div class="tagg">
        <ul class="menu" id="menu-%e5%8f%8b%e6%83%85%e9%93%be%e6%8e%a5">
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1605" id="menu-item-1605">
          <a href="http://taidizh.com/">
           泰迪智慧
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20884" id="menu-item-20884">
          <a href="http://www.transwarp.cn/">
           星环科技
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-3538" id="menu-item-3538">
          <a href="http://datall.org/">
           珈和遥感
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20888" id="menu-item-20888">
          <a href="http://www.chinahadoop.cn/">
           小象学院
          </a>
         </li>
        </ul>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <div class="textwidget">
       <div align="center">
        <a href="http://study.163.com/course/courseMain.htm?courseId=991022" target="_blank">
         <img src="http://dataunion.org/wp-content/uploads/2016/03/dv.jpg"/>
        </a>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       文章分类
      </h4>
      <div class="tagcloud">
       <a class="tag-link-44" href="http://dataunion.org/category/industry/demo" style="font-size: 10.204724409449pt;" title="4个话题">
        Demo展示
       </a>
       <a class="tag-link-31" href="http://dataunion.org/category/experts" style="font-size: 15.826771653543pt;" title="52个话题">
        专家团队
       </a>
       <a class="tag-link-870" href="http://dataunion.org/category/tech/ai" style="font-size: 19.795275590551pt;" title="273个话题">
        人工智能
       </a>
       <a class="tag-link-488" href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f" style="font-size: 8pt;" title="1个话题">
        加入数盟
       </a>
       <a class="tag-link-869" href="http://dataunion.org/category/tech/viz" style="font-size: 17.204724409449pt;" title="93个话题">
        可视化
       </a>
       <a class="tag-link-30" href="http://dataunion.org/category/partners" style="font-size: 10.645669291339pt;" title="5个话题">
        合作伙伴
       </a>
       <a class="tag-link-889" href="http://dataunion.org/category/parterc" style="font-size: 11.582677165354pt;" title="8个话题">
        合作会议
       </a>
       <a class="tag-link-104" href="http://dataunion.org/category/books" style="font-size: 12.96062992126pt;" title="15个话题">
        图书
       </a>
       <a class="tag-link-220" href="http://dataunion.org/category/tech/base" style="font-size: 19.850393700787pt;" title="281个话题">
        基础架构
       </a>
       <a class="tag-link-219" href="http://dataunion.org/category/tech/analysis" style="font-size: 19.409448818898pt;" title="232个话题">
        数据分析
       </a>
       <a class="tag-link-887" href="http://dataunion.org/category/tech/dm" style="font-size: 13.291338582677pt;" title="17个话题">
        数据挖掘
       </a>
       <a class="tag-link-34" href="http://dataunion.org/category/tech" style="font-size: 20.732283464567pt;" title="404个话题">
        文章
       </a>
       <a class="tag-link-1" href="http://dataunion.org/category/uncategorized" style="font-size: 22pt;" title="693个话题">
        未分类
       </a>
       <a class="tag-link-4" href="http://dataunion.org/category/events" style="font-size: 14.503937007874pt;" title="29个话题">
        活动
       </a>
       <a class="tag-link-890" href="http://dataunion.org/category/tech/%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0" style="font-size: 10.204724409449pt;" title="4个话题">
        深度学习
       </a>
       <a class="tag-link-221" href="http://dataunion.org/category/tech/devl" style="font-size: 18.968503937008pt;" title="193个话题">
        编程语言
       </a>
       <a class="tag-link-888" href="http://dataunion.org/category/career" style="font-size: 15.661417322835pt;" title="48个话题">
        职业规划
       </a>
       <a class="tag-link-5" href="http://dataunion.org/category/jobs" style="font-size: 14.11811023622pt;" title="25个话题">
        职位
       </a>
       <a class="tag-link-871" href="http://dataunion.org/category/industry" style="font-size: 15.716535433071pt;" title="49个话题">
        行业
       </a>
       <a class="tag-link-613" href="http://dataunion.org/category/industry/case" style="font-size: 16.984251968504pt;" title="84个话题">
        行业应用
       </a>
       <a class="tag-link-885" href="http://dataunion.org/category/industry/news" style="font-size: 17.425196850394pt;" title="102个话题">
        行业资讯
       </a>
       <a class="tag-link-10" href="http://dataunion.org/category/training" style="font-size: 14.228346456693pt;" title="26个话题">
        课程
       </a>
       <a class="tag-link-16" href="http://dataunion.org/category/sources" style="font-size: 15.661417322835pt;" title="48个话题">
        资源
       </a>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       功能
      </h4>
      <ul>
       <li>
        <a href="http://dataunion.org/wp-login.php?action=register">
         注册
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/wp-login.php">
         登录
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/feed">
         文章
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/comments/feed">
         评论
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="https://cn.wordpress.org/" title="基于WordPress，一个优美、先进的个人信息发布平台。">
         WordPress.org
        </a>
       </li>
      </ul>
     </div>
    </aside>
    <div class="clear">
    </div>
   </div>
   <!--main-->
   ﻿
   <footer id="dibu">
    <div class="about">
     <div class="right">
      <ul class="menu" id="menu-%e5%ba%95%e9%83%a8%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-18024" id="menu-item-18024">
        <a href="http://dataunion.org/category/partners">
         合作伙伴
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20881" id="menu-item-20881">
        <a href="http://dataunion.org/contribute">
         文章投稿
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20872" id="menu-item-20872">
        <a href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f">
         加入数盟
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22441" id="menu-item-22441">
        <a href="http://dataunion.org/f-links">
         友情链接
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20874" id="menu-item-20874">
        <a href="http://dataunion.org/aboutus">
         关于数盟
        </a>
       </li>
      </ul>
      <p class="banquan">
       数盟社区        ，
        做最棒的数据科学社区
      </p>
     </div>
     <div class="left">
      <ul class="bottomlist">
       <li>
        <a href="http://weibo.com/DataScientistUnion  " target="_blank" 　title="">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weibo.png"/>
        </a>
       </li>
       <li>
        <a class="cd-popup-trigger" href="http://dataunion.org/19838.html#0">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weixin.png"/>
        </a>
       </li>
      </ul>
      <div class="cd-popup">
       <div class="cd-popup-container">
        <h1>
         扫描二维码,加微信公众号
        </h1>
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/2014-12-06-1515289049.png"/>
        <a class="cd-popup-close" href="http://dataunion.org/19838.html">
        </a>
       </div>
       <!-- cd-popup-container -->
      </div>
      <!-- cd-popup -->
     </div>
    </div>
    <!--about-->
    <div class="bottom">
     <a href="http://dataunion.org/">
      数盟社区
     </a>
     <a href="http://www.miitbeian.gov.cn/" rel="external nofollow" target="_blank">
      京ICP备14026740号
     </a>
     联系我们：
     <a href="mailto:contact@dataunion.org" target="_blank">
      contact@dataunion.org
     </a>
     <div class="tongji">
     </div>
     <!--bottom-->
     <div class="scroll" id="scroll" style="display:none;">
      ︿
     </div>
    </div>
   </footer>
   <!--dibu-->
  </div>
 </body>
</html>